#!/bin/bash
Docker_tools="docker_tools"
KERNEL_RPM="KERNEL_RPM"
DebugTools="DebugTools"
Debug_std="DebugTools/standard"
Debug_stg="DebugTools/storage"
make_tools="${CPATH}/make_tools"
yum_conf="${CPATH}/repo_conf/obs-aarch64.conf"
yum_conf_storage="${CPATH}/repo_conf/obs-pangea.conf"

function make_debug_tools()
{
    chroot_run "cd /home; bash make_version.sh make_debug_tools_inchroot"
}


function make_debug_tools_inchroot()
{
    rm -rf $CPATH/$DebugTools
    mkdir -p $CPATH/$Debug_std
    mkdir -p $CPATH/$Debug_stg    

    log_info "==========Start DebugTools vmcore_debug_tool====standard======"
    vmcore_tool_dir="vmcore_debug_tool"
    vmcore_tool_tar="vmcore_debug_tool.tar.gz"
    get_vmcoretool $WORK_DIR"repos/Euler" $make_tools/vmcore_debug_tool.rpmlist $vmcore_tool_dir $vmcore_tool_tar "standard"
    if [ -f "./$vmcore_tool_tar" ]; then
        create_checksum $vmcore_tool_tar
        cp -ap $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM} ./$Debug_std/
        rm -f $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM}
    else
        log_error "no $vmcore_tool_tar"
        exit 1
    fi

    log_info "==========Start DebugTools vmcore_debug_tool====storage======"
    vmcore_tool_dir="vmcore_debug_tool"
    vmcore_tool_tar="vmcore_debug_tool.tar.gz"
    get_vmcoretool $WORK_DIR"repos/Euler" $make_tools/vmcore_debug_tool_storage.rpmlist $vmcore_tool_dir $vmcore_tool_tar "storage"
    if [ -f "./$vmcore_tool_tar" ]; then
        create_checksum $vmcore_tool_tar
        cp -ap $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM} ./$Debug_stg/
        rm -f $vmcore_tool_tar "$vmcore_tool_tar"${SHA256SUM}
    else
        log_error "no $vmcore_tool_tar"
        exit 1
    fi 

   log_info "==========Start DebugTools debug_tool======================="
    debug_tool_dir="debug_tool"
    debug_tool_tar="debug_tool.tar.gz"
    make_tool $WORK_DIR"repos/Euler" $make_tools/debug_tool.rpmlist $debug_tool_dir $debug_tool_tar
    if [ -f "./$debug_tool_tar" ]; then
        create_checksum $debug_tool_tar
        cp -ap $debug_tool_tar "$debug_tool_tar"${SHA256SUM} ./$DebugTools/
        rm -f $debug_tool_tar "$debug_tool_tar"${SHA256SUM}
    else
        log_error "no $debug_tool_tar"
        exit 1
    fi

    log_info "==========Start DebugTools perf_tool======================="
    perf_tool_dir="perf_tool"
    perf_tool_tar="perf_tool.tar.gz"
    make_tool $WORK_DIR"repos/Euler" $make_tools/perf_tool.rpmlist $perf_tool_dir $perf_tool_tar
    if [ -f "./$perf_tool_tar" ]; then
        create_checksum $perf_tool_tar
        cp $perf_tool_tar "$perf_tool_tar"${SHA256SUM} ./$DebugTools/
        rm $perf_tool_tar "$perf_tool_tar"${SHA256SUM}
    else
        log_error "no $perf_tool_tar"
        exit 1
    fi

    RELEASEDIR=`get_release_dir`
    RELEASE_DIR="${RELEASEDIR}/${SUB_RELEASE_DIR}/"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "$DebugTools" "$RELEASE_DIR"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
}

function get_vmcoretool()
{
    el_repo=$1
    vmcorelst=$2
    tooldir=$3
    tooltar=$4
    product=$5

    if [ ! -f "$vmcorelst" ];then
        log_error "not found $vmcorelst"
        return 1
    fi
    rm -rf $tooldir
    mkdir $tooldir
    mkdir $tooldir/tmp
    #python $TOOLS/repo_maker.py -f $vmcorelst -d $tooldir/tmp -p "${OBSSERVER_IP}:82"
    yum clean all -c $yum_conf
    yum clean all -c $yum_conf_storage
    if [ "x$product" = "xstandard" ];then
        yumdownloader -c $yum_conf --destdir=$tooldir/tmp --archlist aarch64 `cat $vmcorelst | tr '\n' ' '`
    elif [ "x$product" = "xstorage" ];then
        yumdownloader -c $yum_conf_storage --destdir=$tooldir/tmp --archlist aarch64 `cat $vmcorelst | tr '\n' ' '`
    fi
    
    if [ $? -ne 0 ]; then
        log_error "when get vmcore debug rpm failed"
        return 1
    fi
    cd $tooldir
    cd tmp

    #vmcore_list=$(ls ./Euler/*.rpm)
    vmcore_list=$(ls ./*.rpm)
    if [ $? -ne 0 ];then
        echo "mktool.sh: function [get_vmcoretool] [$LINENO]: rpm is not exist. please download it yourself." && return 1
    fi
    for rpm in ${vmcore_list[*]}
    do
        rpm2cpio $rpm | cpio -id
        rm -rf $rpm
    done
    
    if [ "x$product" = "xstorage" ];then
        cp -p usr/lib64/libsnappy.so.1  ../
        cp -p usr/lib64/liblzo2.so.2  ../
        echo "#!/bin/bash" > ../install.sh
        echo "cp ./lib*  /usr/lib64/" >> ../install.sh
        chmod 700 ../install.sh
    fi
    cp -p usr/bin/crash ../
    cp -p usr/lib/debug/lib/modules/*/vmlinux ../
    cd -
    rm -rf tmp
    tar -zcf ../$tooltar *
    cd ..
    rm -rf $tooldir
}

function make_tool()
{
    el_repo=$1
    rpmlst=$2
    tooldir=$3
    tooltar=$4

    if [ ! -f "$rpmlst" ];then
        log_error "not found $rpmlst"
            return 1
    fi

    rm -rf $tooldir
    mkdir $tooldir
    #python $TOOLS/repo_maker.py -f $rpmlst -d $tooldir -p "${OBSSERVER_IP}:82"
    yum clean all -c $yum_conf
    yumdownloader -c $yum_conf --destdir=$tooldir --archlist=noarch,aarch64 `cat $rpmlst | tr '\n' ' '`

    if [ $? -ne 0 ]; then
        log_error "when download rpm failed"
            return 1
    fi
    cd $tooldir

    #rpm_list=$(ls ./Euler/*.rpm)
    rpm_list=$(ls ./*.rpm)
    if [ $? -ne 0 ];then
        echo "mktool.sh: function [make_tool] [$LINENO]: rpm is not exist. please download it yourself." && return 1
    fi
    for rpm in ${rpm_list[*]}
    do
            rpm2cpio $rpm | cpio -id
        rm -rf $rpm
    done

    rm -rf Euler

    rm -rf usr/share/doc usr/share/man
    if [ -d bin ];then
        if [ ! -d usr/bin ];then
                mkdir -p usr/bin
        fi
        cp -ap bin/* usr/bin/
        rm -rf bin
    fi
    if [ -d sbin ];then
        if [ ! -d usr/sbin ];then
                mkdir -p usr/sbin
        fi
        cp -ap sbin/* usr/sbin/
        rm -rf sbin
    fi
    if [ -d lib ];then
        if [ ! -d usr/lib ];then
                mkdir -p usr/lib
        fi
        cp -ap lib/* usr/lib/
        rm -rf lib
    fi
    if [ -d lib64 ];then
        if [ ! -d usr/lib64 ];then
                mkdir -p usr/lib64
        fi
        cp -ap lib64/* usr/lib64/
        rm -rf lib64
    fi

    tar -zcf ../$tooltar *
    cd ..
    rm -rf $tooldir
}

function docker_tools()
{
    chroot_run "cd /home; bash make_version.sh docker_tools_inchroot"
}

function docker_tools_inchroot()
{
    log_info "==========Start get_kernel_rpm=========="
    LIST="DockerStack.lst"
    mkdir $Docker_tools
    cd $Docker_tools
    yum clean all -c $yum_conf
    yum list --installroot=${CPATH}/tmp available -c $yum_conf | awk '{print $1}' > ava_lst
    for rpmlist in $LIST
    do
        for rname in `cat $make_tools/$rpmlist`
        do
           if ! grep -w "^$rname" ava_lst > /dev/null 2>&1; then
                log_error "can not find $rname failed"
            fi
        done
        rpm_dir=`echo $rpmlist | sed 's/.lst//g'`
        mkdir $rpm_dir
        yumdownloader -c $yum_conf --disablerepo=obs-7 --destdir=$rpm_dir `cat $make_tools/$rpmlist | tr '\n' ' '`
    done
    pushd DockerStack
    wget -q -r -nd -np -k -L -p -A "docker-engine-[0-9]*.rpm" http://${OBSSERVER_IP}:82/EulerOS:/V3R1:/STANDARD:/Docker/standard_aarch64/aarch64/
    wget -c -r -nd -np -k -L -p -A "docker-engine-selinux-[0-9]*.rpm" http://${OBSSERVER_IP}:82/EulerOS:/V3R1:/STANDARD:/Docker/standard_aarch64/aarch64/
    popd

    cd ./DockerStack
    ls . | while read line
    do
        create_checksum  $line
    done
    cd -
    RELEASEDIR=`get_release_dir`
    RELEASE_DIR="${RELEASEDIR}/${SUB_RELEASE_DIR}/"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "DockerStack" "$RELEASE_DIR"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
}

function make_other_tools()
{
    log_info "==========Start get_kernel_rpm=========="
    get_kernel_rpm
    cd $KERNEL_RPM
    for dir in `ls ./`
    do
        if [ -d $dir ]; then
            cd $dir
            for file in `ls ./`
            do
                create_checksum $file
            done
            cd ..
        fi
    done
    cd ..

    RELEASEDIR=`get_release_dir`
    RELEASE_DIR="${RELEASEDIR}/${SUB_RELEASE_DIR}/"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "$KERNEL_RPM" "$RELEASE_DIR"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
}


function get_kernel_rpm()
{
    REPO="KERNEL_RPM"
    rm -rf $REPO
    python $TOOLS/repo_maker.py -f $make_tools/kernel_rpm_list.xml -d $REPO -p "${OBSSERVER_IP}:82"
    if [ $? -ne 0 ]; then
        log_error "Error: when get kernelrpm"
    fi
}

function container_tools()
{
    chroot_run "cd /home; bash make_version.sh container_tools_inchroot"
}

function container_tools_inchroot
{
    RELEASEDIR=`get_release_dir`
    b_version=`echo $RELEASEDIR | awk -F '_' '{print $NF}' | awk -F '-' '{print $1}'`
    tar_name="elara_${b_version}_binary"

    mkdir -p rpm_dir_tmp
    rm -rf rpm_dir_tmp/*
    cd rpm_dir_tmp
    wget -q -c -r -nd -np -k -L -p -A "elara-1.*" http://${OBSSERVER_IP}:82/EulerOS:/V3R1:/STANDARD:/Docker/standard_aarch64/aarch64/
    rpm2cpio elara-1.*.rpm | cpio -div
    cd -

    if [ -d $tar_name ];then
        rm -rf ${tar_name}*
    fi
    cp -a ./rpm_dir_tmp/var/lib/elara-tar $tar_name
    for line in `ls $tar_name`
    do
        if [ -d ${tar_name}/${line} ];then
            chmod 750 ${tar_name}/${line}
        fi
    done
    chmod 750 ${tar_name}
    tar -cf $tar_name.tar ./$tar_name
    sha256sum $tar_name.tar > $tar_name.tar${SHA256SUM}

    RELEASE_DIR="${RELEASEDIR}/${SUB_RELEASE_DIR}/DockerStack"
    SSH_CMD="mkdir -p $RELEASE_DIR"
    sshcmd "$SSH_CMD"
    sshscp "$tar_name.tar $tar_name.tar${SHA256SUM}" "${RELEASE_DIR}"
    chmod_http
    if [ $? -ne 0 ]; then
        log_error "Failed in chmod_http"
    fi
}
